<?php

class icsDB extends mysqli {
	
	// single instance of self shared among all instances
	private static $instance = null;
	// db connection config vars
	private $user   = "phpuser";
	private $pass   = "phpuserpw";
	private $dbName = "ics";
	private $dbHost = "localhost";
	private $con    = null;
	
	//This method must be static, and must return an instance of the object if the object
	//does not already exist.
	public static function getInstance() {
		if (!self :: $instance instanceof self) {
			self::$instance = new self;
		}
		return self::$instance;
	}
	
	// The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
        // thus eliminating the possibility of duplicate objects.
	public function __clone() {
		trigger_error('Clone is not allowed. ', E_USER_ERROR);
	}
	
	public function __wakeup(){
		trigger_error('Deserializing is not allowed. ', E_USER_ERROR);
	}
	
	// private constructor
	private function __construct() {
		parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
		if (mysqli_connect_error()) {
			exit('Connect Error (' . mysqli_connect_errno() . ') '
					. mysqli_connect_error());
		}
		parent::set_charset('utf-8');
	}
	
	public function get_warehouse_id_by_name($name) {
		$name = $this->real_escape_string($name);
		$item = $this->query("SELECT id FROM warehouse WHERE i_name = '"
				. $name . "'");
		
		if ($item->num_rows > 0){
			$row = $item->fetch_row();
			return $row[0];
		} else
			return null;
	}
		
	public function get_products_by_warehouse_id($warehouseID) {
		return $this->query("SELECT id,i_name,i_cost,i_qoh FROM warehouse WHERE warehouse.id=" . $warehouseID);
	}

	public function create_customer($name, $password,$street,$city,$state,$country,$zip) {
		$name = $this->real_escape_string($name);
		$password = $this->real_escape_string($password);
		$street = $this->real_escape_string($street);
		$city = $this->real_escape_string($city);
		$state = $this->real_escape_string($state);
		$country = $this->real_escape_string($country);
		$zip = $this->real_escape_string($zip);
		
		
		$this->query("INSERT INTO customers (cname, cpassword,cstreet,ccity,cstate,ccountry,czip) VALUES ('" . $name
				. "', '" . $password . "', '" . $street . "', '" . $city . "', '" . $state . "', '" . $country . "', '" . $zip . "')");
	}
	
	public function verify_customer_credentials($name, $password) {
        $name = $this->real_escape_string($name);
        $password = $this->real_escape_string($password);
        $result = $this->query("SELECT 1 FROM customers WHERE name = '"
                        . $name . "' AND password = '" . $password . "'");
        return $result->data_seek(0);
    }
	
	 function insert_product($warehouseID, $name, $cost,$qoh) {
        $name = $this->real_escape_string($name);
        $cost = $this->real_escape_string($cost);
        $qoh = $this->real_escape_string($qoh);	
        if ($this->format_date_for_sql($cost)==null){
           $this->query("INSERT INTO inventory (wid, iname)" .
                " VALUES (" . $warehouseID . ", '" . $name . "')");
        } else
        $this->query("INSERT INTO inventory (wid, iname,icost,iqoh)" .
                " VALUES (" . $warehouseID . ", '" . $name . "', '" . $cost . "', '" . $qoh . "', "
                . $this->format_date_for_sql($duedate) . ")");
    }
    
    function format_date_for_sql($date) {
        if ($date == "")
            return null;
        else {
            $dateParts = date_parse($date);
            return $dateParts['year'] * 10000 + $dateParts['month'] * 100 + $dateParts['day'];
        }
    }

    public function update_product($iID, $name, $cost,$qoh) {
        $name = $this->real_escape_string($name);
        $cost = $this->real_escape_string($cost);
        $qoh = $this->real_escape_string($qoh);	
        $this->query("UPDATE inventory SET iname = '" . $name .
                "', icost = " . $this->$cost . " ',iqoh = " .$this->$qoh 
                . " WHERE iid =" . $iID);
    }

    public function get_product_by_warehouse_id($warehouseID) {
        return $this->query("SELECT iid, iname,icost,iqoh FROM inventory WHERE iid = " . $warehouseID);
    }

    public function delete_product($productID) {
        $this->query("DELETE FROM inventory WHERE iid = " . $productID);
    }

    public function get_customer_id_by_name($name) {
	$name = $this->real_escape_string($name);
	$customer = $this->query("SELECT id FROM customers WHERE cname = '"
			. $name. "'");
	if ($customer->num_rows > 0){
		$row = $customer->fetch_row();
		return $row[0];
	} else
		return null;
	}
}


?>